const userLevelModel = require('../../model/userLevelModel');
const { check_number, check_id, check_string,check_page } = require('../../utils/checkType');
const { getFormatDate } = require('../../utils/dataUtils')

//添加用户等级
async function addLevel(req, res, next) {
    let { discount, exp_num, explain, grade, icon, image, is_show, name } = req.body
    if (check_number(discount,res,'discount') && check_number(exp_num,res,'exp_num') && explain && check_string(explain,res,'explain') && check_number(grade,res,'grade') && icon && check_string(icon,res,'icon') && image && check_string(image,res,'image') && name && check_string(name,res,'name') && check_number(is_show,res,'is_show')) {
        let add_time = getFormatDate()
        let data = await userLevelModel.create({ discount, exp_num, explain, grade, icon, image, is_show, name, add_time, is_del: 0 })
        res.send({
            meta: {
                msg: '添加成功',
                status: 200
            }
        })
    } else {
        res.send({
            meta: {
                msg: '参数有误',
                status: 202
            }
        })
    }
}
//查询用户等级
async function find(req, res, next) {
    let params = req.body
    let data = await userLevelModel.find({ is_del: 0 })
    if(params._id){
        check_id(params._id)
        data = data.filter(item=> item._id == params._id)
    }
    if(params.name){
        check_string(params.name)
        data = data.filter(item=> item.name == params.name)
    }
    if(params.is_show){
        data = data.filter(item=> item.is_show == params.is_show)
    }
    
    //分页
    let total = data.length
    if (check_page(params.limit)&&check_page(params.page)) {
        data = data.filter((item, index) => index < params.limit * params.page && index >= params.limit * (params.page - 1))
    } else if(params.limit && params.page) {
        res.send({
            meta: {
                msg: 'limit,page参数错误',
                status: 202
            }
        });
        return false
    }
    res.send({
        data,
        total,
        meta: {
            msg: '查询成功',
            status: 200,
        }
    });
    return false;

}

//删除用户等级
async function del(req, res, next) {
    let { _id } = req.body
    if (check_id(_id,res,'id')) {
        let data = await userLevelModel.updateMany({ _id }, { is_del: 1 })
        if (data.nModified) {
            res.send({
                meta: {
                    msg: '删除成功',
                    status: 200
                }
            })
        } else {
            res.send({
                meta: {
                    msg: '删除失败',
                    status: 201
                }
            })
        }
    } else {
        res.send({
            meta: {
                msg: '参数有误',
                status: 202
            }
        })
    }
}
//修改用户等级
async function update(req, res, next) {
    let { _id, name, discount, exp_num, explain, grade, icon, image, is_show } = req.body
    if (check_id(_id,res,'id') && name && check_string(name,res,'name') && check_number(discount,res,'discount') && check_number(exp_num,res,'exp_num') && check_number(grade,res,'grade') && check_number(is_show,res,'is_show') && explain && check_string(explain,res,'explain') && icon && check_string(icon,res,'icon') && image && check_string(image,res,'image')) {
        let data = await userLevelModel.updateMany({ _id }, { name, discount, exp_num, explain, grade, icon, image, is_show })
        if (data.nModified) {
            res.send({
                meta: {
                    status: 200,
                    msg: "修改成功"
                }
            })
        } else {
            res.send({
                meta: {
                    status: 201,
                    msg: "修改失败"
                }
            })
        }

    } else {
        res.send({
            meta: {
                status: 202,
                msg: "参数有误"
            }
        })
    }
}

async function findOther(req, res, next) {

}

module.exports = {
    addLevel,
    find,
    del,
    update,
    findOther
}